<?php


namespace App\Http\Controllers\Admin\BlindDate;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class CupidController extends BaseController
{
    /**
     * @api {post} /blind-date/cupid/prop 新增丘比特道具
     *
     * @apiGroup 相亲-丘比特
     *
     * @apiParam {String} prop_name 道具名称，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} icon 图标，必须
     * @apiParam {String} arrow 箭图，必须
     * @apiParam {Int} level 等级，必须
     * @apiParam {Int} price 价格，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function propAdd(Request $request)
    {
        $param = $request->only('prop_name', 'picture', 'icon', 'arrow', 'level', 'price');
        $v = app('validator')->make($param,[
            'prop_name' => 'required|string|max:10',
            'picture' => 'required|url|max:255',
            'icon' => 'required|url|max:255',
            'arrow' => 'required|url|max:255',
            'level' => 'required|integer',
            'price' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_blind_date')->addOrEditCupidProp($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /blind-date/cupid/prop 编辑丘比特道具
     *
     * @apiGroup 相亲-丘比特
     *
     * @apiParam {int} id 序号，必须
     * @apiParam {String} prop_name 道具名称，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} icon 图标，必须
     * @apiParam {String} arrow 箭图，必须
     * @apiParam {Int} level 等级，必须
     * @apiParam {Int} price 价格，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function propEdit(Request $request)
    {
        $param = $request->only('id', 'prop_name', 'picture', 'icon', 'arrow', 'level', 'price');
        $v = app('validator')->make($param,[
            'id' => 'required|integer|min:1',
            'prop_name' => 'required|string|max:10',
            'picture' => 'required|url|max:255',
            'icon' => 'required|url|max:255',
            'arrow' => 'required|url|max:255',
            'level' => 'required|integer',
            'price' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_blind_date')->addOrEditCupidProp($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {get} /blind-date/cupid/prop 丘比特道具列表
     *
     * @apiGroup 相亲-丘比特
     *
     * @apiParam {Int} page 第几页，默认1
     * @apiParam {Int} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 2, 序号
    "name": "丘比特之箭2", 名称
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/60b098f4b16d8.png", 图片
    "icon": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/60b098f4b16d8.png", 图标
    "arrow": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/60b098f4b16d8.png", 箭图
    "level": 2, 等级
    "price": 2000 价格
    }
    ],
    "first_page_url": "http://local.qipaoadmin/blind-date/cupid/prop?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.qipaoadmin/blind-date/cupid/prop?page=1",
    "next_page_url": null,
    "path": "http://local.qipaoadmin/blind-date/cupid/prop",
    "per_page": 10,
    "prev_page_url": null,
    "to": 1,
    "total": 1
    }
    }
     */
    public function propList(Request $request)
    {
        $param = $request->only('page', 'page_size');
        $v = app('validator')->make($param,[
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $pageSize = $param['page_size'] ?? 10;

        $list = app('model_game_dating_cupid')
            ->select('id', 'name', 'picture', 'icon', 'arrow', 'level', 'price', 'create_time')
            ->where('is_delete', 0)
            ->paginate($pageSize);
        foreach ($list as $value){
            $value->create_time = empty($value->create_time) ? '' : date('Y-m-d H:i:s', $value->create_time);
        }
        return Response::sendData($list);
    }

    /**
     * @api {delete} /blind-date/cupid/prop 删除丘比特道具
     *
     * @apiGroup 相亲-丘比特
     *
     * @apiParam {String} ids 要删除的记录的id，多个用英文逗号连接
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "deleted_count": 1 删除成功的条数
    }
    }
     */
    public function propDel(Request $request)
    {
        $ids = $request->ids;

        if (empty($ids)) throw new BasicException(0, '请选择要删除的道具');

        $ids = explode(',', $ids);

        $res = app('model_game_dating_cupid')->whereIn('id', $ids)->update(['is_delete' => 1]);
        if ($res) {
            return Response::sendData(['deleted_count' => $res]);
        }
        throw new BasicException(0, '删除失败');
    }
}